﻿var xmlHttp = null;
var oldId;
function createXMLHttpRequest() {
    if(window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if(window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function doJSON(url) {
    createXMLHttpRequest();
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            parseResults();
        }
    }
}
function parseResults() {
	var text = xmlHttp.responseText;
	
	if(text == "[]") {
		$("#board_list").empty();
		$("#board_list").append("<p style='text-align:center'>아직 등록된 글이 없습니다^^</p>");
	}
	
	else {
		var json = $.parseJSON(xmlHttp.responseText);
		$("#board_list").empty();
		//서버에서 반환한 json포맷의 data를 테이블로 변환시켜준다.

		// insertion sort
		
		for(var index = 1; index < json.length; index++) {
			var temp = json[index];
			var aux = index - 1;
			
			while((aux >=0) && (eval(json[aux]['num']) < eval(temp['num']))) {
				json[aux+1] = json[aux];
				aux--;
			}
			
			json[aux+1] = temp;
		}
		
		
		$.each(json, function(entryIndex, entry) {
			var num = entry['num'];
			var subject = entry['subject'];
			var regdate = entry['regdate'];
			var content = entry['content'];
			var filename1 = entry['filename1'];
			var filetype1 = entry['filetype1'];
			
			var now = new Date();
			var cmpdate = parseDate(regdate);
			
			if(subject.charAt(0) == '\[') {
				
			} else {
				var html = "<div class = \"board_item\">";
				
				html += subject;
				
	
				html += "<div class = heart style=float:right><img class=btn src=image/bt-close.png></div>";
				
				if(now.getYear() == cmpdate.getYear()) {
					if(now.getDate() - cmpdate.getDate() < 3 && now.getDate() - cmpdate.getDate() > -1 ) {
						html += "<div class = new><img src=image/bt_new.png></div>";
					}
				}
				
				var year = cmpdate.getYear() + 1900;
				var month = cmpdate.getMonth() + 1 ;
				//html += "<div class = regdate>" + parseInt(year) + "." + parseInt(month) + "." + parseInt(cmpdate.getDate()) + "</div>";
				
				html += "</div>";	// end board item
				
				// set div tag as board_content
				html += "<div class = \"board_content\">";
				
				// load image from json if content has picture type file
				if(filename1 != "" && ( filetype1 == "jpg" || filetype1 == "gif" || filetype1 == "JPG" || filetype1 == "GIF" || filetype1 == "PNG" || filetype1 == "png" )){
					html += "<div class = \"board_image\">";
					html += "<img src = \"http://www.btjcenter.com/upload/"+ filename1 +"\" width=100% BORDER=\"0\" align=\"absmiddle\"/>";
					html += "</div>";
				}
				// load content from json
				
				var p_content = "<p>" + content + "</p>";
				
				p_content = p_content.replace(/\r\n\r\n/gi, "</p><p>");
				p_content = p_content.replace(/\r\n \r\n/gi, "</p><p>");
				p_content = p_content.replace(/\r\n/gi, "</p><p>");
				
			
				html += p_content;
				
				html += "</div>"; // end of board_content
				
				
				$("#board_list").append(html);
				$(".board_content").hide();
			}
		});
		
		$(".board_item").click(function(event) {
			var submenu = $(this).next("div");

			if( submenu.is(":visible") ){
				submenu.slideUp();
				$(this).find("img.btn").attr("src","image/bt-close.png");
			}else{
				submenu.slideDown();
				$(this).find("img.btn").attr("src","image/bt-open.png");
			}
		});
	}
	
}

function parseDate(input, format) {
	format = format || 'yyyy-mm-dd'; // default format
	var parts = input.match(/(\d+)/g), 
	i = 0, fmt = {};
	
	// extract date-part indexes from the format
	format.replace(/(yyyy|dd|mm)/g, function(part) { fmt[part] = i++; });

	return new Date(parts[fmt['yyyy']], parts[fmt['mm']]-1, parts[fmt['dd']]);
}
